using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GamePlay.Data;
using UnityEngine;
using UnityEngine.UI;

namespace IQIGame.Onigao.GamePlay
{
    public class UIPortableItem : UIEntityWindow
    {
        #region CreateConfig

        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIPortableItem),
            layer = EUILayer.Normal,
            activeBlurBg = true,
            blurDownSampling = 4,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;

        #endregion

        #region Template Generate,don't modify

        protected class UIB_UIPortableItem
        {
            #region ObjectBinding Generate

            public ExButton btnMask { protected set; get; }
            public ExLoopVerticalScrollRect loopVerticalScrollRect { protected set; get; }
            public GameObject panelRight { protected set; get; }
            public ExButton btnConfirm { protected set; get; }
            public ExButton btnCancel { protected set; get; }
            public ExButton btnClose { protected set; get; }

            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<ExButton>("btnMask", out var __tbv0);
                btnMask = __tbv0;
                __binding.TryGetVariableValue<ExLoopVerticalScrollRect>("loopVerticalScrollRect", out var __tbv1);
                loopVerticalScrollRect = __tbv1;
                __binding.TryGetVariableValue<GameObject>("panelRight", out var __tbv2);
                panelRight = __tbv2;
                __binding.TryGetVariableValue<ExButton>("btnConfirm", out var __tbv3);
                btnConfirm = __tbv3;
                __binding.TryGetVariableValue<ExButton>("btnCancel", out var __tbv4);
                btnCancel = __tbv4;
                __binding.TryGetVariableValue<ExButton>("btnClose", out var __tbv5);
                btnClose = __tbv5;
            }

            #endregion ObjectBinding Generate
        }

        #endregion Template Generate,don't modify

        #region Field

        private UD_PortableItem _uiData => _data as UD_PortableItem;
        private LoopScrollAdapter<UIN_PortableItem> _loopListHandle;
        private UIN_PortableItemPanel _panel;

        private ItemData _selectedPortableItemData;

        #endregion

        #region Interface

        protected UIB_UIPortableItem ui { get; set; }

        protected override void BeforeInit()
        {
            ui = new UIB_UIPortableItem();
            ui.InitBinding(csObjBind);
        }

        protected override void OnInit()
        {
            _loopListHandle = InitLoopScroll<UIN_PortableItem>(ui.loopVerticalScrollRect, RefreshItem);
            UIN_PortableItem.onItemSelected = OnItemSelected;
            _panel = InitChildNode<UIN_PortableItemPanel>(ui.panelRight);
        }

        protected override void OnShow()
        {

        }

        protected override void OnHide()
        {

        }

        protected override void AddUGUIListener()
        {
            ui.btnClose.onClick.AddListener(OnBtnCloseClick);
            ui.btnMask.onClick.AddListener(OnBtnCloseClick);
            ui.btnConfirm.onClick.AddListener(OnConfirmClick);
            ui.btnCancel.onClick.AddListener(OnCancelClick);
        }

        protected override void OnShow(UIBaseData data)
        {
            if (data is not UD_PortableItem uiData)
            {
                return;
            }

            OnItemSelected(uiData.defaultItemData);
            _loopListHandle.RefillCells(_uiData.portableItems.Count);
        }

        protected override void OnDispose()
        {
        }

        #endregion

        #region Method

        private void OnItemSelected(ItemData data)
        {
            _selectedPortableItemData = data;
            _panel.Show();
            _panel.Refresh(data);
            _loopListHandle.RefreshCells();
        }

        private void RefreshItem(UIN_PortableItem portableItem, int index)
        {
            portableItem.Refresh(_uiData.portableItems[index], _selectedPortableItemData.cid);
        }

        private void OnBtnCloseClick()
        {
            Close();
        }

        private void OnConfirmClick()
        {
            Switch(_selectedPortableItemData?.id ?? 0);
        }

        private void OnCancelClick()
        {
            Switch(0);
        }

        private void Switch(long itemId)
        {
            ItemModule.NetCore.CS_setPortableItem(itemId).Forget();
            Close();
        }

        #endregion
    }
}